package xiegd.study.lambda;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Description:
 * @Author: xieguangda
 * @Date: 2021年07月21日 10:53
 * 需求：
 */
public class Study2 {

    public static Map<Integer,List<String>> userIdAndUserRole = new HashMap<Integer,List<String>>();
    {
        userIdAndUserRole.put(1,new ArrayList<String>(){{
            add("admin");
            add("student");
        }});
        userIdAndUserRole.put(2,new ArrayList<String>(){{
            add("admin");
            add("teacher");
        }});
        userIdAndUserRole.put(3,new ArrayList<String>(){{
            add("teacher");
            add("student");
        }});
    }

    public static void main(String[] args) {
        Study2 s = new Study2();
        s.getUserInfo();
    }

    /**
     * 根据userList里面的userId,查找userIdAndUserRole里面的用户角色，包含admin的留下来，别的去掉，并把userRole赋值
     */
    public void getUserInfo(){
        List<UserInfo> userList = new ArrayList<UserInfo>();
        userList.add(new UserInfo(1,"测试1",""));
        userList.add(new UserInfo(2,"测试2",""));
        userList.add(new UserInfo(3,"测试3",""));


        List<UserInfo> collect = userList.stream().map(item -> {
            List<String> list = userIdAndUserRole.get(item.getUserId());
            if (list.contains("admin")) {
                item.setUserRole("admin");
            }
            return item;
        }).filter(item -> item.getUserRole().equals("admin")).collect(Collectors.toList());


        System.out.println("collect = " + collect);

    }
}


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
class UserInfo{
    private Integer userId;
    private String userName;
    private String userRole;
}
